Recognize RUSTC/RUSTDOC environment variables
authorAlex Crichton <alex@alexcrichton.com>
Mon, 18 May 2015 21:55:42 +0000 (14:55 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 18 May 2015 22:01:35 +0000 (15:01 -0700)
src/cargo/ops/cargo_rustc/context.rs
src/cargo/ops/cargo_rustc/engine.rs
src/cargo/ops/cargo_rustc/mod.rs
src/cargo/util/config.rs
src/cargo/util/mod.rs
src/doc/config.md
tests/test_cargo_compile.rs

index cb52e9446f5885f798d5b183644c6ae4e067d69d..65e612f8fce49eea3028ce4d770e184762d840b8 100644 (file)
@@ -98,7 +98,7 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
     /// specified as well as the exe suffix
     fn filename_parts(target: Option<&str>)
                       -> CargoResult<(Option<(String, String)>, String)> {
-        let mut process = try!(util::process("rustc"));
+        let mut process = try!(util::process(util::rustc()));
         process.arg("-")
                .arg("--crate-name").arg("_")
                .arg("--crate-type").arg("dylib")
index 75c4672173b51cbd6a4bc1b590cea407d0642ecb..3d3a17d8ba7ba195d27cf9b343ecea8dcc65c8d8 100644 (file)
@@ -4,7 +4,7 @@ use std::fmt;
 use std::path::Path;
 use std::process::Output;
 
-use util::{CargoResult, ProcessError, ProcessBuilder, process};
+use util::{self, CargoResult, ProcessError, ProcessBuilder, process};
 
 /// Trait for objects that can execute commands.
 pub trait ExecEngine: Send + Sync {
@@ -38,8 +38,8 @@ impl CommandPrototype {
     pub fn new(ty: CommandType) -> CargoResult<CommandPrototype> {
         Ok(CommandPrototype {
             builder: try!(match ty {
-                CommandType::Rustc => process("rustc"),
-                CommandType::Rustdoc => process("rustdoc"),
+                CommandType::Rustc => process(util::rustc()),
+                CommandType::Rustdoc => process(util::rustdoc()),
                 CommandType::Target(ref s) |
                 CommandType::Host(ref s) => process(s),
             }),
index 64e2fd2755d36f918b587a7ca7f65c9815ebee5e..5986693d019a15776e54065b8f9aa788e3862078 100644 (file)
@@ -57,7 +57,7 @@ pub struct TargetConfig {
 /// The second element of the tuple returned is the target triple that rustc
 /// is a host for.
 pub fn rustc_version() -> CargoResult<(String, String)> {
-    let output = try!(try!(util::process("rustc"))
+    let output = try!(try!(util::process(util::rustc()))
         .arg("-vV")
         .exec_with_output());
     let output = try!(String::from_utf8(output.stdout).map_err(|_| {
index a6479e8183817ee66e24b83ac58252eb4a3c2840..f2a592530b4a249227c5e195288ca5e165daa56f 100644 (file)
@@ -394,6 +394,14 @@ fn homedir() -> Option<PathBuf> {
     return cargo_home.or(user_home);
 }
 
+pub fn rustc() -> String {
+    env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string())
+}
+
+pub fn rustdoc() -> String {
+    env::var("RUSTDOC").unwrap_or_else(|_| "rustdoc".to_string())
+}
+
 fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
     where F: FnMut(File, &Path) -> CargoResult<()>
 {
index e29fa5844541d6f0cbb047f00419ab8d476ad72b..ea71ae78076f814278638b902ca6f3b6009e7930 100644 (file)
@@ -1,4 +1,4 @@
-pub use self::config::Config;
+pub use self::config::{Config, rustc, rustdoc};
 pub use self::process_builder::{process, ProcessBuilder};
 pub use self::errors::{CargoResult, CargoError, ChainError, CliResult};
 pub use self::errors::{CliError, ProcessError};
index d48c9dddda28228c75e1c37fd5ed06e434235b90..32b6d3d51ecc9059eb6541ecd1034f960bd0d791 100644 (file)
@@ -79,9 +79,14 @@ timeout = 60000   # Timeout for each HTTP request, in milliseconds
 jobs = 1        # number of jobs to run by default (default to # cpus)
 ```
 
-# Configuration of registry cache
+# Environment Variables
 
-Cargo maintains a local cache of the registry index and of git
-checkouts of crates.  By default these are stored under
-`$HOME/.cargo`. The location can be overridden by setting the
-`CARGO_HOME` environment variable.
+Cargo recognizes a few global environment variables to configure how it runs:
+
+* `CARGO_HOME` - Cargo maintains a local cache of the registry index and of git
+  checkouts of crates.  By default these are stored under `$HOME/.cargo`, but
+  this variable overrides the location of this directory.
+* `RUSTC` - Instead of running `rustc`, Cargo will execute this specified
+  compiler instead.
+* `RUSTDOC` - Instead of running `rustdoc`, Cargo will execute this specified
+  `rustdoc` instance instead.
index c8bd75f6a8d12335a17fda3ae15c2d0e4f0758c5..0a50d770e9ba65519840f7b20f283014b8b6bde7 100644 (file)
@@ -1758,6 +1758,29 @@ test!(dashes_in_crate_name_bad {
                 execs().with_status(101));
 });
 
+test!(rustc_env_var {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "");
+    p.build();
+
+    assert_that(p.cargo("build")
+                 .env("RUSTC", "rustc-that-does-not-exist").arg("-v"),
+                execs().with_status(101)
+                       .with_stderr("\
+Could not execute process `rustc-that-does-not-exist -vV` ([..])
+
+Caused by:
+[..]
+"));
+    assert_that(&p.bin("a"), is_not(existing_file()));
+});
+
 test!(filtering {
     let p = project("foo")
         .file("Cargo.toml", r#"